package com.itany.crawler;

import com.itany.utils.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.UUID;

public class SecondHandle extends UrlBaseHandle {
    static String url_sec = "http://www.bookschina.com//kinder/54000000/";


    public SecondHandle(String url, boolean proxyed) {
        super(url, proxyed);
    }

    public SecondHandle() {
        setUrl(url_sec);
    }

    @Override
    public String handle(Urls urls) throws Exception {

        //获取页面html代码
        String html = doGet();
        //将html字符串解析成dom对象
        Document document = Jsoup.parse(html, "UTF-8");
        Log.logErr("开启解析下一页 :" + getUrl());
        try {
            Element bookList = document.getElementsByClass("bookList").get(0);
            Elements lis = bookList.getElementsByTag("li");
            lis.forEach(li -> {
                Elements names = li.getElementsByClass("name");
                names.forEach(name -> {
                    Elements links = name.getElementsByTag("a");
                    String href = links.get(0).attr("href");
                    String text = links.get(0).text();
                    System.out.println(href + "  " + text);
                    String absUrl = "http://www.bookschina.com/" + href;
                    ThirdHandle thirdHandle = new ThirdHandle(absUrl, isProxyed());
                    urls.addLast(thirdHandle);
                });
            });

        } catch (Exception e) {
            Log.logErr("解析二级页面数据失败 :" + getUrl());
            throw new CustomException("解析二级页面失败");
        }


        // 添加下一页、
        addNextPage(urls, document);
        return null;
    }

    private void addNextPage(Urls urls, Document document) {
        try {
            Element next = document.getElementsByClass("nextPage").get(0);
//            String href = next.getElementsByTag("a").get(0).attr("href");
            String href = next.attr("href");
            String absUrl = "http://www.bookschina.com/" + href;
            SecondHandle handle = new SecondHandle(absUrl, isProxyed());
            urls.addLast(handle);
            Log.logErr("添加下一页成功:" + absUrl);
        } catch (Exception e) {
            Log.logErr("解析下一页出错:" + getUrl());
            String fileName = UUID.randomUUID().toString();
            Log.logErr("文件名 :" + fileName);
            Log.logHtml(fileName, document.html());
        }
    }


    public static void main(String[] args) throws Exception {
        SecondHandle handle = new SecondHandle(url_sec, false);
        Urls urls = new Urls();
        handle.handle(urls);
    }

}
